package de.hawhamburg.shc.controller;


import java.util.ArrayList;
import java.util.HashMap;

import de.hawhamburg.smarthomecontroller.R;
import de.hawhamburg.shc.interfaces.ISpeechFragment;
import de.hawhamburg.shc.interfaces.ISpeechRecognizer;
import de.hawhamburg.shc.livingplace.WlanManager;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;


public class MainActivity extends FragmentActivity implements ActionBar.TabListener, ISpeechRecognizer  {
	/*
	 * The PagerAdapter that will provide fragments for each of the sections. 
	 */
	SectionsPagerAdapter mSectionsPagerAdapter;
	
	//Object Variables for Speech recognizion. 
	private static int requestCode = 0; 	
	private HashMap<Integer , ISpeechFragment> callingFragments = new HashMap<Integer, ISpeechFragment>(); 
	private WlanManager wlanManager;
	private String smartHomeWlan = "LivingplaceSensor";
	private String internetWlan =  "HAW.1X";
	
	// The ViewPager that will host the section contents.
	ViewPager mViewPager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Settings settings = new Settings(MainActivity.this);
		wlanManager = new WlanManager(this);
		
		// Set up the action bar.
		final ActionBar actionBar = getActionBar();
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
	
		// Create the adapter that will return a fragment for each of the two
		// primary sections of the app.
		mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(),settings);

		// Set up the ViewPager with the sections adapter.
		mViewPager = (ViewPager) findViewById(R.id.pager);
		mViewPager.setAdapter(mSectionsPagerAdapter);

		// When swiping between different sections, select the corresponding tab.
		mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
			@Override
			public void onPageSelected(int position) {
				actionBar.setSelectedNavigationItem(position);
			}
		});

		// For each of the sections in the app, add a tab to the action bar.
		for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
			// Create a tab with text corresponding to the page title defined by
			// the adapter. Also specify this Activity object, which implements
			// the TabListener interface, as the callback (listener) for when
			// this tab is selected.
			actionBar.addTab(actionBar.newTab().setText(mSectionsPagerAdapter.getPageTitle(i)).setTabListener(this));
		}
	}
	
	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void onTabSelected(ActionBar.Tab tab,FragmentTransaction fragmentTransaction) {
		// When the given tab is selected, switch to the corresponding page in the ViewPager.
		mViewPager.setCurrentItem(tab.getPosition());
	}


	@Override
	public void onTabReselected(Tab tab, FragmentTransaction ft) {
		// TODO Auto-generated method stub
		
	}



	@Override
	public void onTabUnselected(Tab tab, FragmentTransaction ft) {
		// TODO Auto-generated method stub
		
	}
	
	 @Override
		protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		   super.onActivityResult(requestCode, resultCode, data);
		   if(wlanManager.connectTo(smartHomeWlan)){
			   if(callingFragments.containsKey(requestCode)) 	
		 	{
		 		if(data != null)
		 		{
		 			ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
		 			if(!results.isEmpty())
		 			{
		 				callingFragments.get(requestCode).result(results);
		 			}
		 		}
		 		
		 	}}
		}


	@Override
	public void recognizSpeech(ISpeechFragment callingFragment) {
		    if(wlanManager.connectTo(internetWlan)){
			this.callingFragments.put(requestCode, callingFragment);
		    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
			intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);      		
			startActivityForResult(intent, requestCode);    	
		    }
		    else{
		    	callingFragment.errorInSpeechRecognion("es konnte keine Verbindung mit dem internetWlan " + internetWlan + " hergestellt werden! Abbruch des intents");
		    }
		}
}
